﻿using Protocol.Code;
using Protocol.Dto;
using Script.Character;
using Script.Framework;
using Script.Model;
using UnityEngine.UI;

namespace Script.UI._2.Fight
{
    public class MyStatePanel : StatePanel
    {
        protected override void Awake()
        {
            Bind(UIEvent.SHOW_CRAB_BUTTON,
                UIEvent.SHOW_DEAL_BUTTON,
                UIEvent.SET_MY_PLAYER_DATA,
                UIEvent.PLAYER_HIDE_READY_BUTTON);
        }

        public override void Execute(int eventCode, object message)
        {
            base.Execute(eventCode, message);

            switch (eventCode)
            {
                case UIEvent.SHOW_CRAB_BUTTON:
                {
                    bool active = (bool) message;
                    mBtnGrab.gameObject.SetActive(active);
                    mBtnNGrab.gameObject.SetActive(active);
                    break;
                }
                case UIEvent.SHOW_DEAL_BUTTON:
                {
                    bool active = (bool) message;
                    mBtnDeal.gameObject.SetActive(active);
                    mBtnNDeal.gameObject.SetActive(active);
                    break;
                }
                case UIEvent.SET_MY_PLAYER_DATA:
                {
                    userDto = message as UserDto;
                    break;
                }
                case UIEvent.PLAYER_HIDE_READY_BUTTON:
                {
                    mBtnReady.gameObject.SetActive(false);
                    break;
                }
            }
        }


        // UI Object
        public Button mBtnDeal;
        public Button mBtnNDeal;
        public Button mBtnGrab;
        public Button mBtnNGrab;
        public Button mBtnReady;

        private SocketMsg socketMsg;


        protected override void Start()
        {
            base.Start();

            initVariable();

            initListener();

            initData();

            initView();
        }


        private void initData()
        {
            UserDto myUserDto = Models.GameModel.MatchRoomDto.UIdUserDict[Models.GameModel.UserDto.Id];
            userDto = myUserDto;
        }

        private void initView()
        {
            // 初始化状态
            mBtnGrab.gameObject.SetActive(false);
            mBtnNGrab.gameObject.SetActive(false);
            mBtnDeal.gameObject.SetActive(false);
            mBtnNDeal.gameObject.SetActive(false);
        }

        private void initListener()
        {
            // 注册事件
            mBtnDeal.onClick.AddListener(DealClick);
            mBtnNDeal.onClick.AddListener(NoDealClick);
            mBtnGrab.onClick.AddListener(
                delegate { GrabClick(true); });

            mBtnNGrab.onClick.AddListener(
                delegate { GrabClick(false); });

            mBtnReady.onClick.AddListener(ReadyClick);
        }

        private void initVariable()
        {
            // 获取对象
            mBtnDeal = transform.Find("btnDeal").GetComponent<Button>();
            mBtnNDeal = transform.Find("btnNDeal").GetComponent<Button>();
            mBtnGrab = transform.Find("btnGrab").GetComponent<Button>();
            mBtnNGrab = transform.Find("btnNGrab").GetComponent<Button>();
            mBtnReady = transform.Find("btnReady").GetComponent<Button>();


            // 创建对象
            socketMsg = new SocketMsg();
        }

        protected override void ReadyState()
        {
            base.ReadyState();
            mBtnReady.gameObject.SetActive(false);
        }


        private void DealClick()
        {
            // 通知角色模块发送出牌请求
            Dispatch(AreaCode.CHARACTER, CharacterEvent.DEAD_CARD, null);


            // 必须让服务器判断的结果后在改变UI
            // 不然可能应为网络时间差 覆盖代码
            // 隐藏出牌和不出牌的按键
            // mBtnDeal.gameObject.SetActive(false);
            // mBtnNDeal.gameObject.SetActive(false);
        }

        private void NoDealClick()
        {
            // 发送不出牌
            socketMsg.Change(OpCode.FIGHT, FightCode.PASS_CREQ, null);

            Dispatch(AreaCode.NET, 0, socketMsg);

            // 必须让服务器判断的结果后在改变UI
            // 不然可能应为网络时间差 覆盖代码
            // 隐藏出牌和不出牌的按键
            // mBtnDeal.gameObject.SetActive(false);
            // mBtnNDeal.gameObject.SetActive(false);
        }

        private void GrabClick(bool result)
        {
            // 抢地主
            socketMsg.Change(OpCode.FIGHT, FightCode.GRAB_LANDLORD_CREQ, result);
            Dispatch(AreaCode.NET, 0, socketMsg);


            // 隐藏抢地主的按钮
            // 更好的方法是让服务器返回消息之后在隐藏
            mBtnGrab.gameObject.SetActive(false);
            mBtnNGrab.gameObject.SetActive(false);
        }


        private void ReadyClick()
        {
            // 向服务器发送准备
            socketMsg.Change(OpCode.MATCH, MatchCode.READY_CREQ, null);

            Dispatch(AreaCode.NET, 0, socketMsg);
        }

        public override void OnDestroy()
        {
            base.OnDestroy();

            mBtnDeal.onClick.RemoveListener(DealClick);
            mBtnNDeal.onClick.RemoveListener(NoDealClick);
            mBtnGrab.onClick.RemoveAllListeners();
            mBtnNGrab.onClick.RemoveAllListeners();
            mBtnReady.onClick.RemoveListener(ReadyClick);
        }
    }
}